<?php

class User extends Controller{
	
	function _passwordAbstraction($usertype){
		$data = array();
		$rules['password'] = "required|alpha_dash";
		$rules['new_password'] = "required|min_length[8]|alpha_dash";
		$rules['new_password_conf'] = "required|matches[new_password]";
		$this->validation->set_rules($rules);
		
		$fields['password'] = "Password:";
		$fields['new_password'] = "New Password:";
		$fields['new_password_conf'] = "Retype New Password:";
		$this->validation->set_fields($fields);
		
		if($this->validation->run()){
			$new["password"] = sha1($this->input->post("password"));
			$new["new_password"] = sha1($this->input->post("new_password"));
			
			$this->load->model("usersettings");
			$this->UserSettings->changePassword($userid, $usertype);
		}
		
		$password["login"] = $this->load->view("gate/login", $data, TRUE);
		$password["body"] = $this->load->view("user/password", $data, TRUE);
		$this->load->view("home", $password);
	}
	
	function editProfile($usertype, $specifics = ""){
		$data = array();
		if($usertype == "teacher"){
			if($specifics == "password"){
				$this->_passwordAbstraction('t');
			} else{
				$rules['lastname'] = "alpha|max_length[50]";
				$rules['firstname'] = "alpha|max_length[50]";
				$rules['middlename'] = "alpha|max_length[50]";
				//And then sex which is set at the view
				$rules['mobilenumber'] = "numeric|exact_length[11]"; //I'm torn over my rules on this one. Hindi ako sigurado sa numeric
				//And then birthday which is set at the view
				$rules['photourl'] = "max_length[255]|aplha_dash"; //alpha_dash or wala na lang validation?
				$rules['aboutme'] = "max_length[255]";
				$rules['academiccredentials'] = "max_length[255]";
				$this->validation->set_rules($rules);
				
				$fields['lastname'] = "Last Name:";
				$fields['firstname'] = "First Name:";
				$fields['middlename'] = "Middle Name:";
				$fields['mobilenumber'] = "Mobile no.:";
				$fields['photourl'] = "Photo URL:";
				$fields['aboutme'] = "About Me:";
				$fields['academiccredentials'] = "Academic Credentials:";
				$this->validation->set_fields($fields);
			
				if($this->validation->run()){
					$new["lastname"] = $this->input->post("lastname");
					$new["firstname"] = $this->input->post("firstname");
					$new["middlename"] = $this->input->post("middlename");
					$new["mobilenumber"] = $this->input->post("mobilenumber");
					$new["photourl"] = $this->input->post("photourl");
					$new["aboutme"] = $this->input->post("aboutme");
					$new["academiccredentials"] = $this->input->post("academiccredentials");
					
					$this->load->model("usersettings");
					$this->UserSettings->saveStudentDetails($_SESSION["userid"]); //Paano ko malalaman ang userid
				}
				
				$teacher["login"] = $this->load->view("gate/login", $data, TRUE);
				$teacher["body"] = $this->load->view("user/teacher", $data, TRUE);
				$this->load->view("home", $teacher);
			}
		} else if($usertype == "student"){
			if($specifics == "password"){
				$this->_passwordAbstraction('s');
			} else{
				$rules['lastname'] = "alpha|max_length[50]";
				$rules['firstname'] = "alpha|max_length[50]";
				$rules['middlename'] = "alpha|max_length[50]";
				$rules['address'] = "max_length[100]|alpha_dash";
				$rules['occupation'] = "max_length[100]";
				$this->validation->set_rules($rules);
				
				$fields['lastname'] = "Last Name:";
				$fields['firstname'] = "First Name:";
				$fields['middlename'] = "Middle Name:";
				$fields['address'] = "Street Address:";
				$fields['occupation'] = "Occupation:";
				$this->validation->set_fields($fields);
				
				if($this->validation->run()){
					$new["lastname"] = $this->input->post("lastname");
					$new["firstname"] = $this->input->post("firstname");
					$new["middlename"] = $this->input->post("middlename");
					$new["address"] = $this->input->post("address");
					$new["occupation"] = $this->input->post("occupation");
					
					$this->load->model("usersettings");
					$this->UserSettings->saveTeacherDetails($userid); //again...
				}	
				
				$student["login"] = $this->load->view("gate/login", $data, TRUE);
				$student["body"] = $this->load->view("user/student", $data, TRUE);
				$this->load->view("home", $student);
			}
		} else{
			//Raise a 404? :|
		}
	}
}
?>
